package com.crm.dao.saling;

import com.crm.pojo.Back;
import com.crm.vo.BackClientContractUserInfoVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/10
 */
public interface BackDao extends CrudRepository<Back, Long> {
    /**
     * 获取所有的回款列表
     *
     * @return
     */
    @Query("from Back b where b.backIsDel='否' ")
    public List<Back> showBackList(Pageable pageable);

    /**
     * 逻辑删除回款
     *
     * @param backId
     */

    @Query("update Back b set b.backIsDel='是' where b.backId=?1")
    @Modifying
    public void deleteBackByBackId(Long backId);

    /**
     * 查询对应合同的回款记录
     *
     * @param contractId
     * @return
     */
    @Query("select back from Back back,PlaningBack planingBack where back.fkBackPlaningBackId=planingBack.planingBackId and planingBack.fkPlaningBackOrderId=?1 and back.backIsDel='否' ")
    public List<Back> getBackRecordByContractId(BigDecimal contractId);

    /**
     * 查询一个未删除的回款记录
     *
     * @param backId
     * @return
     */
    @Query("from Back back where back.backIsDel='否' and back.backId=?1")
    public Back getBackByBackId(Long backId);


    /**
     * 获取回款列表
     *
     * @param pageable
     * @return
     */
    @Query("select new com.crm.vo.BackClientContractUserInfoVO(back,clientInfo,contract,userInfo) from PlaningBack planingBack,Back back,Contract contract,Userinfo userInfo,ClientInfo clientInfo " +
            "where back.fkBackPlaningBackId=planingBack.planingBackId and planingBack.fkPlaningBackOrderId=contract.contractId and planingBack.planingBackIsDel='否'and back.backIsDel='否' " +
            "and contract.fkCustomerId=clientInfo.cid and contract.contractEmpno=userInfo.uId")
    public List<BackClientContractUserInfoVO> getBackClientContractUserInfoVOList(Pageable pageable);

    /**
     * 获取回款记录的数量
     *
     * @return
     */
    @Query("select count(back) from Back  back where back.backIsDel='否'")
    public Long getBackCount();
}
